package com.jxtc.enterprise.hr.controller;

import com.jxtc.enterprise.hr.dto.CompanyDTO;
import com.jxtc.enterprise.hr.service.HrCompanyManageService;
import com.jxtc.enterprise.common.utils.Result;
import com.jxtc.enterprise.common.utils.ResultGenerator;
import com.jxtc.enterprise.hr.service.HrDepartmentManagementService;
import com.jxtc.enterprise.hr.vo.CompanyDetailVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/jxtc/enterprise/company")
@RequiredArgsConstructor
@Slf4j
public class CompanySettingController {

    private final HrCompanyManageService hrCompanyManageService;

    @Autowired
    private final HrDepartmentManagementService hrDepartmentManagementService;

    @ApiResponse(responseCode = "200", description = "若响应体中 code 为 200 表示查询公司信息成功，data 字段为公司信息")
    @ApiResponse(responseCode = "200", description = "若响应体中 code 为 400 表示查询公司信息失败，message 字段则会提供错误信息")
    @Parameter(name = "id", description = "公司 ID", example = "c752804d0cd410267f404dd3f0457a57")
    @Operation(summary = "9.1 根据公司 ID 查询公司配置信息", tags = "9. 公司配置", operationId = "9.1")
    @GetMapping()
    public Result<CompanyDetailVO> queryCompanyDetailById(String id) {
        CompanyDetailVO vo = hrCompanyManageService.queryById(id);
        return ResultGenerator.genSuccessResult(vo);
    }

    @ApiResponse(responseCode = "200", description = "若响应体中 code 为 200 表示修改公司配置信息成功，data 字段为空")
    @ApiResponse(responseCode = "200", description = "若响应体中 code 为 400 表示修改公司配置信息失败，message 字段则会提供错误信息")
    @Operation(summary = "9.2 修改公司配置", tags = "9. 公司配置", operationId = "9.2")
    @PutMapping("/settings")
    public Result<Void> updateCompanyInfo(@Valid @RequestBody CompanyDTO companyDTO) {
        log.info("{}", companyDTO);
        hrCompanyManageService.update(companyDTO);
        // update department set address = #{} where company_id = #{}
        hrDepartmentManagementService.batchUpateDepartPickUpAddress(companyDTO.getId(), companyDTO.getAddress());

        return ResultGenerator.genSuccessResult();
    }
}
